from morsecode import *

#print(create_code())



class morse_code():
    def __init__(self):
        self.code, self.sh_code = create_code()
        self.usr_mode = ''
        self.usr_input = ''
        #以下为编码模式下所需变量
        self.en = ''
        self.word = ''
        self.EN_list = []
        self.b_index = ''
        self.b_code_list = []
        self.t = ''
        #解码
        self.usr_mode = ''
        self.m_code_list = []
        self.j_eng_list = []
        self.j_eng = ''
        #临时变量
        #self.k = 0
    
    def usr_setup(self):
        update = input('u或U代表检查升级,其他代表跳过更新：')
        if update == 'u' or update == 'U':
            from webbrowser import open
            from time import sleep
            print('跳转更新中...')
            sleep(1)
            open('https://afdian.net/a/alpharadio?tab=feed')
            open('https://www.zhihu.com/people/alpharadio')
            open('https://user.qzone.qq.com/3764780278/')

        self.usr_mode = input('B或b代表电码编码，J或j代表电码解码：')
        if self.usr_mode == 'B' or self.usr_mode == 'b':
            self.usr_mode = 'b'
            print('编码模式')
            self.b_morsecode()
        elif self.usr_mode == 'J' or self.usr_mode == 'j':
            self.usr_mode = 'j'
            print('解码模式')
            self.j_morsecode()
        else:
            self.usr_setup()


    def b_en2EN(self):
        self.EN_list = []
        #print('清空self.enlist')#
        self.en = input('请输入要编码的英文：')
        self.t = input('d/D 代表短码模式，其他值均为长码模式：')
        if self.t != 'd' or self.t != 'D':
            #print(self.en)#
            #print(self.EN_list)#
            self.EN_list = []
            #print(self.EN_list)#
            for item in self.en:
                if ord(item) >= 97 and ord(item) <= 122:
                    #print('1')
                    self.EN_list.append(chr(ord(item) - 32))
                elif ord(item) >= 65 and ord(item) <= 90:
                    #print('2')
                    self.EN_list.append(item)
                elif ord(item) >= 48 and ord(item) <= 57:
                    #print('is number!')#
                    self.EN_list.append(item)
                elif ord(item) == 32:
                    self.EN_list.append(item)
                    #print('空格！')
                else:
                    print('在转换字符时遇到问题，当前版本只支持字母！了解更多请联系开发者')
                    break
        else:
            self.EN_list = list(self.en)
        return self.EN_list
    
    def b_morsecode(self):
        self.word = self.b_en2EN()
#        self.word = ''.join(self.word)

        #print(self.EN_list)#
        self.b_code_list = []
        #print(self.t)
        if self.t != 'd' and self.t != 'D':
            #print('long')#
            self.b_code_list = []
            for s in self.word:
                try:
                    if ord(s) >= 48 and ord(s) <= 57:
                        #print('is number!1')#
                        self.b_index = self.code[25 + (ord(s) - 47)][1]

                        #print(25 + (ord(s) - 47))#
                        #print(self.code[28])#
                    elif ord(s) == 32:
                        self.b_index = ' '
                    else:
                        #print('eng')
                        self.b_index = self.code[ord(s) - 65][1]
                    #print(self.b_index)#
                except:
                    print('编码长码时出现错误：一般为电码输入不正确，若电码为正确但报该错误，请向我们反馈。')
                #print(self.b_index)#
                self.b_code_list.append(self.b_index)
        else:
            #print('short code!')#
            #print('短码编解码暂不支持空格')
            for s in self.word:
                self.b_index = ' '
                try:
                    for m in range(9):

                        if s == self.sh_code[m][0]:
                            self.b_index = self.sh_code[m][1]
                            #print(self.b_index)#
                except:
                    print('编码长码时出现错误：一般为电码输入不正确，若电码为正确但报该错误，请向我们反馈。')
                self.b_code_list.append(self.b_index)
        #print(self.b_code_list)#
        print('电码是：')
        print('/'.join(self.b_code_list))

    def j_morsecode(self):
        self.m_code_list = self.j_split_code()
        self.j_eng_list = []
        if self.t2 != 'd' and self.t2 != 'D':

            try:

                for s in self.m_code_list:
                    #print(s, end=' ')#
                    if s == ' ':
                        self.j_eng_list.append(' ')
                
                    for k in range(36):
                        #print(k)#
                        if self.code[k][1] == s:
                            #print('断点1')#
                            self.j_eng_list.append(self.code[k][0])                        
            except IndexError:
                print('在解码时遇到问题，可能有未记录的电码或错误的格式！')
                #raise e
        else:
            try:
                for s in self.m_code_list:
                    if s == ' ':
                        self.j_eng_list.append(' ')
                    else:
                        for j in range(9):
                            if self.sh_code[j][1] == s:
                                self.j_eng_list.append(self.sh_code[j][0])
            except IndexError:
                print('在解码时遇到问题，可能有未记录的电码或错误的格式！')
        print('解码后的英文是：')
        #print(self.j_eng_list)#
        print(''.join(self.j_eng_list))

    def j_split_code(self):
        self.m_code = input('请输入要解码的电码，用/斜杠分开：')
        self.t2 = input('d/D 代表短码模式，其他值均为长码模式：')
        try:
            self.m_code_list = self.m_code.split('/')
            #print(self.m_code_list)#
            return self.m_code_list
        except:
            print('解码时遇到错误：未按要求切分单字符电码。')
        
